package com.aegis.gateway.filter;

import com.aegis.gateway.constants.FilterChainEnum;
import com.aegis.gateway.service.FilterManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
@Slf4j
@Component
public abstract class BaseWebFilter implements WebFilter, Ordered {

    @Autowired
    protected FilterManageService filterManageService;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        FilterChainEnum nextFilter = filterManageService.getNextFilter(exchange);
        //当前过滤器不是下一个要执行的过滤器
        if (nextFilter != null && this.getClass() != nextFilter.getFilterClass()) {
            return chain.filter(exchange);
        }
        log.info("===> filter start: {}", getClass().getName());
        Mono<Void> voidMono = doFilter(exchange, chain);
        return voidMono;
    }

    abstract Mono<Void> doFilter(ServerWebExchange exchange, WebFilterChain chain);


}
